#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
#![allow(rustdoc::missing_crate_level_docs)]

use eframe::{CreationContext, egui, Frame};
use eframe::egui::Context;
use crate::utils::load_fonts::{configure_text_style, load_fonts};

mod utils;

fn main() -> eframe::Result<()> {
    // 创建视口选项，设置视口的内部大小为320x240像素
    let options = eframe::NativeOptions {
        viewport: egui::ViewportBuilder::default().with_inner_size([800.0, 600.0]),
        ..Default::default()
    };

    // 运行egui应用程序
    eframe::run_native(
        "My egui App", // 应用程序的标题
        options, // 视口选项
        Box::new(|cc| {
            // 创建并返回一个实现了eframe::App trait的对象
            Ok(Box::new(MyApp::new(cc)))
        }),
    )
}
//导航
enum Page {
    Test,
    Settings,
}
struct MyApp {
    name: String,
    age: i32,
    page: Page,
}
impl MyApp {
    fn new(cc: &CreationContext) -> Self {
        load_fonts(&cc.egui_ctx);
        configure_text_style(&cc.egui_ctx);
        Self {
            name: "".to_string(),
            age: 0,
            page: Page::Test,
        }
    }
    /// 左侧导航
    fn left_ui(&mut self, ui: &mut egui::Ui) {
        //垂直布局，内部水平居中
        ui.vertical_centered_justified(|ui| {
            if ui.button("测试").clicked() {
                self.page = Page::Test;
            }
            if ui.button("设置").clicked() {
                self.page = Page::Settings;
            }
        });
    }

    /// 根据导航显示页面
    fn show_page(&mut self, ui: &mut egui::Ui) {
        match self.page {
            Page::Test => {
                self.test_ui(ui);
            }
            Page::Settings => {
                self.setting_ui(ui);
            }
        }
    }

    fn test_ui(&mut self, ui: &mut egui::Ui) {
        ui.label("这个就是test_ui");
    }

    fn setting_ui(&mut self, ui: &mut egui::Ui) {
        ui.heading("我的egui应用");

        ui.horizontal(|ui| {
            let name_label = ui.label("你得名字:");
            ui.text_edit_singleline(&mut self.name).labelled_by(name_label.id);
        });

        ui.add(egui::Slider::new(&mut self.age, 0..=120).text("年龄"));

        if ui.button("增加").clicked() {
            self.age += 1;
        }

        let msg = format!("你好 '{}', 年龄 '{}'", self.name, self.age);
        ui.label(msg);
    }

    fn main_ui(&mut self, ui: &mut egui::Ui) {
        // 添加面板的顺序非常重要，影响最终的布局
        egui::TopBottomPanel::top("top_panel")
            .resizable(false)
            .min_height(32.0)
            .show_inside(ui, |ui| {
                egui::ScrollArea::vertical().show(ui, |ui| {
                    ui.vertical_centered(|ui| {
                        ui.heading("标题栏");
                    });
                    ui.label("标题栏内容");
                });
            });

        egui::SidePanel::left("left_panel")
            .resizable(false)
            .default_width(150.0)
            .width_range(80.0..=200.0)
            .show_inside(ui, |ui| {
                ui.vertical_centered(|ui| {
                    ui.heading("左导航栏");
                });
                egui::ScrollArea::vertical().show(ui, |ui| {
                    self.left_ui(ui);
                });
            });

        egui::SidePanel::right("right_panel")
            .resizable(false)
            .default_width(150.0)
            .width_range(80.0..=200.0)
            .show_inside(ui, |ui| {
                ui.vertical_centered(|ui| {
                    ui.heading("右导航栏");
                });
                egui::ScrollArea::vertical().show(ui, |ui| {
                    ui.label("右导航栏内容");
                });
            });

        egui::TopBottomPanel::bottom("bottom_panel")
            .resizable(false)
            .min_height(0.0)
            .show_inside(ui, |ui| {
                ui.vertical_centered(|ui| {
                    ui.heading("状态栏");
                });
                ui.vertical_centered(|ui| {
                    ui.label("状态栏内容");
                });
            });

        egui::CentralPanel::default().show_inside(ui, |ui| {
            ui.vertical_centered(|ui| {
                ui.heading("主面板");
            });
            egui::ScrollArea::vertical().show(ui, |ui| {
                ui.label("主面板内容");

                self.show_page(ui);
            });
        });
    }
}

impl eframe::App for MyApp {
    fn update(&mut self, ctx: &Context, _frame: &mut Frame) {
        egui::CentralPanel::default().show(ctx, |ui| {
            // 防止内部文字被选中
            ui.style_mut().interaction.selectable_labels = false;
            self.main_ui(ui);
        });
    }
}